package com.ccc.scala.base7_collection

import scala.collection.mutable.ArrayBuffer


object Collection05_FlatMap {
  def main(args: Array[String]): Unit = {
    // 集合 - flatMap
    val array = ArrayBuffer(
      ArrayBuffer(1, 2), ArrayBuffer(3, 4)
    )

    val array2 = ArrayBuffer(
      ArrayBuffer(
        ArrayBuffer(1, 2), ArrayBuffer(3, 4)
      ), ArrayBuffer(
        ArrayBuffer(5, 6), ArrayBuffer(7, 8)
      )
    )

    println(array) // ArrayBuffer(ArrayBuffer(1, 2), ArrayBuffer(3, 4))

    // 将整体拆分成个体的操作，称之为扁平化
    println(array.flatten) // ArrayBuffer(1, 2, 3, 4)
    // 扁平化只能对最外层进行操作
    println(array2.flatten) // ArrayBuffer(ArrayBuffer(1, 2), ArrayBuffer(3, 4), ArrayBuffer(5, 6), ArrayBuffer(7, 8))


    // flatMap
    val array3 = ArrayBuffer(
      "Hello Scala", "Hello Hadoop"
    )
    println(array3.flatten) // ArrayBuffer(H, e, l, l, o,  , S, c, a, l, a, H, e, l, l, o,  , H, a, d, o, o, p)

    println(array3.flatMap(str => {
      str.split(" ")
    })) // ArrayBuffer(Hello, Scala, Hello, Hadoop)
    // 简化
    println(array3.flatMap(_.split(" "))) // ArrayBuffer(Hello, Scala, Hello, Hadoop)


  }

}
